package edu.cebanc.spring.biblioteca.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.domain.Author;

@Repository("authorsDAO")
@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
public class AuthorsDAOimpl implements AuthorsDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void saveAuthor(Author author) {
		sessionFactory.getCurrentSession().saveOrUpdate(author);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Author> listAuthors() {
		return (List<Author>) sessionFactory.getCurrentSession().createCriteria(Author.class).list();
	}

	@Override
	public Author getAuthor(int id) {
		return (Author) sessionFactory.getCurrentSession().load(Author.class, id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void delAuthor(Author author) {
		sessionFactory.getCurrentSession().delete(author);
	}

}
